/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.Documento;
import bean.Factura;
import java.sql.*;
import java.util.ArrayList;

/**
 *
 * @author LiziTa
 */
class SQLServerDAOFacturacion implements DAOFacturacion {

    public float igv;
    @Override
    public ArrayList<Factura> queryAll() {
        
        ArrayList <Factura> facturas = new ArrayList<Factura>();
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Factura f;
        
        try {            
            Driver myDriver = new com.microsoft.sqlserver.jdbc.SQLServerDriver();
            String connectionUrl =  "jdbc:sqlserver://inti.lab.inf.pucp.edu.pe:1433;"+
                                    "databaseName=inf282;user=inf282;password=inf282db;";
            conn = DriverManager.getConnection(connectionUrl);
            pstmt = conn.prepareStatement("SELECT * FROM G04_FACTURA");
            rs =  pstmt.executeQuery();
            
            while (rs.next()){
                int idFact = rs.getInt("IDFACTURA");
                int idUser = rs.getInt("IDUSUARIO");
                int idDoc  = rs.getInt("IDDOCUMENTO");
                String estado = rs.getString("ESTADO_PAGO");
                Date fechaE = rs.getDate("FECHA_EMISION");
                Date fechaV = rs.getDate("FECHA_VENCIMIENTO");
                float igv = rs.getFloat("IGV");
                float mora = rs.getFloat("MORA");
                String tipo = rs.getString("TIPO_PAGO");
                String moneda = rs.getString("MONEDA");
                float total = rs.getFloat("TOTAL");
                
                f= new Factura(idFact,idUser,idDoc,estado,fechaE,fechaV,igv,mora,tipo,moneda,total);
                facturas.add(f);
            }            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
             try {if (rs != null) rs.close(); } 
             catch(Exception e){e.printStackTrace();}  
        } 
            return facturas;
    }

    @Override
    public Factura queryByIdF(String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int insert(Factura f) {
        
        int result=0;
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {            
            Driver myDriver = new com.microsoft.sqlserver.jdbc.SQLServerDriver();
            String connectionUrl =  "jdbc:sqlserver://inti.lab.inf.pucp.edu.pe:1433;"+
                                    "databaseName=inf282;user=inf282;password=inf282db;";
            conn = DriverManager.getConnection(connectionUrl);
            pstmt = conn.prepareStatement("INSERT INTO G04_FACTURA"
                    + "(IDFACTURA,IDUSUARIO,IDDOCUMENTO,ESTADO_PAGO,FECHA_EMISION,FECHA_VENCIMIENTO,IGV,MORA,TIPO_PAGO,MONEDA,TOTAL) "
                    + "VALUES(?,?,?,?,?,?,?,?,?,?,?)");
            pstmt.setString(1,""+f.getId());
            pstmt.setString(2,""+f.getIdUser());
            pstmt.setString(3,""+f.getIdDoc());
            pstmt.setString(4,f.getEstado());
            pstmt.setString(5,""+f.getFechaE());
            pstmt.setString(6,""+f.getFechaV());
            pstmt.setString(7,""+f.getIgv());
            pstmt.setString(8,""+f.getMora());
            pstmt.setString(9,""+f.getTipoPago());
            pstmt.setString(10,""+f.getMoneda());
            pstmt.setString(11,""+f.getTotal());
            rs =  pstmt.executeQuery();
                        
            if (result == 0){
                throw new Exception();
            }            
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
             //5. Se cierra la conexión
             try {if (rs != null) rs.close(); } 
             catch(Exception e){e.printStackTrace();}  
        } 
        return result;
    }

    @Override
    public int update(Factura f) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int delete(String idFactura) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Factura> queryByNF(String nombre) {
        throw new UnsupportedOperationException("Not supported yet.");
    }   
}
